package Elasticsearchs

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/Elasticsearchs"
	ElasticsearchsReq "github.com/flipped-aurora/gin-vue-admin/server/model/Elasticsearchs/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type InterviewEsApi struct {
}

var interviewEsService = service.ServiceGroupApp.ElasticsearchsServiceGroup.InterviewEsService

// CreateInterviewEs 创建interview表
// @Tags InterviewEs
// @Summary 创建interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body Elasticsearchs.InterviewEs true "创建interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /interviewEs/createInterviewEs [post]
func (interviewEsApi *InterviewEsApi) CreateInterviewEs(c *gin.Context) {
	var interviewEsEdit Elasticsearchs.InterviewEsEdit
	err := c.ShouldBindJSON(&interviewEsEdit)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	companyName := ""
	tagContent := ""
	for _, val := range interviewEsEdit.CompanyName {
		companyName += val + ","
	}
	for _, val := range interviewEsEdit.TagContent {
		tagContent += val + ","
	}
	fmt.Println(interviewEsEdit)
	fmt.Println(companyName)
	interviewEs := Elasticsearchs.InterviewEs{
		GVA_MODEL:      interviewEsEdit.GVA_MODEL,
		Founder:        interviewEsEdit.Founder,
		InterviewTitle: interviewEsEdit.InterviewTitle,
		Answer:         interviewEsEdit.Answer,
		CompanyName:    companyName[:len(companyName)-1],
		TagContent:     tagContent[:len(tagContent)-1],
		CreatedAt:      interviewEsEdit.CreatedAt,
		UpdatedAt:      interviewEsEdit.UpdatedAt,
		DeletedAt:      interviewEsEdit.DeletedAt,
	}
	if err := interviewEsService.CreateInterviewEs(&interviewEs); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteInterviewEs 删除interview表
// @Tags InterviewEs
// @Summary 删除interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body Elasticsearchs.InterviewEs true "删除interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /interviewEs/deleteInterviewEs [delete]
func (interviewEsApi *InterviewEsApi) DeleteInterviewEs(c *gin.Context) {
	var interviewEs Elasticsearchs.InterviewEs
	err := c.ShouldBindJSON(&interviewEs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewEsService.DeleteInterviewEs(interviewEs); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteInterviewEsByIds 批量删除interview表
// @Tags InterviewEs
// @Summary 批量删除interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /interviewEs/deleteInterviewEsByIds [delete]
func (interviewEsApi *InterviewEsApi) DeleteInterviewEsByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewEsService.DeleteInterviewEsByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateInterviewEs 更新interview表
// @Tags InterviewEs
// @Summary 更新interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body Elasticsearchs.InterviewEs true "更新interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /interviewEs/updateInterviewEs [put]
func (interviewEsApi *InterviewEsApi) UpdateInterviewEs(c *gin.Context) {
	var interviewEs Elasticsearchs.InterviewEs
	err := c.ShouldBindJSON(&interviewEs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := interviewEsService.UpdateInterviewEs(interviewEs); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindInterviewEs 用id查询interview表
// @Tags InterviewEs
// @Summary 用id查询interview表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query Elasticsearchs.InterviewEs true "用id查询interview表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /interviewEs/findInterviewEs [get]
func (interviewEsApi *InterviewEsApi) FindInterviewEs(c *gin.Context) {
	var interviewEs Elasticsearchs.InterviewEs
	err := c.ShouldBindQuery(&interviewEs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reinterviewEs, err := interviewEsService.GetInterviewEs(interviewEs.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reinterviewEs": reinterviewEs}, c)
	}
}

// GetInterviewEsList 分页获取interview表列表
// @Tags InterviewEs
// @Summary 分页获取interview表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query ElasticsearchsReq.InterviewEsSearch true "分页获取interview表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /interviewEs/getInterviewEsList [get]
func (interviewEsApi *InterviewEsApi) GetInterviewEsList(c *gin.Context) {
	var pageInfo ElasticsearchsReq.InterviewEsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := interviewEsService.GetInterviewEsInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
